//
//  ZJXTextView.m
//  ZJXTextView
//
//  Created by ZhuJX on 16/1/18.
//  Copyright © 2016年 ZhuJX. All rights reserved.
//

#import "ZJXTextView.h"

@implementation ZJXTextView{
    UILabel * _placeholderLable;
    float     _textSize;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.placeholderAlwaysShow = NO;//始终显示提示文字 默认为否
    }
    return self;
}

-(void)setFont:(UIFont *)font{//设置TextView字号同时设置placeholder字号
    _placeholderLable.font = font;
    _textSize = font.pointSize;
}

-(float)getDefaultFont{//获取默认字体
    UIFont *newFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    UIFontDescriptor *ctfFont = newFont.fontDescriptor;
    float fontFloat = [[ctfFont objectForKey:@"NSFontSizeAttribute"] floatValue];
    _textSize = fontFloat;
    return fontFloat;
}

-(void)setPlaceholder:(NSString *)placeholder{
    [_placeholderLable removeFromSuperview];
    _placeholderLable      = nil;
    _placeholderLable      = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.frame.size.width - 10, _textSize)];
    _placeholderLable.text = placeholder;
    _placeholderLable.font = [UIFont systemFontOfSize:[self getDefaultFont]];
    [self addSubview:_placeholderLable];
}

-(void)setPlaceholderTextColor:(UIColor *)placeholderTextColor{

    _placeholderLable.textColor = [UIColor colorWithRed:(200)/255.0f green:(200)/255.0f blue:(205)/255.0f alpha:1];
}

-(void)textViewDidChange:(UITextView *)textView{
    self.text.length > 0 ? ( self.placeholderAlwaysShow == YES ? (_placeholderLable.alpha = 1) : ( _placeholderLable.alpha = 0 ) ) : (_placeholderLable.alpha = 1);
}

-(void)setPsition:(placeholderPosition)psition{
    if(psition == placeholderPositionDefault){
        _placeholderLable.frame = CGRectMake(5, 5, self.frame.size.width - 10, _textSize);
    }
    if(psition == placeholderPositionTopLeft){
        _placeholderLable.frame = CGRectMake(5, 5, self.frame.size.width - 10, _textSize);
    }
    if(psition == placeholderPositionRightBottom){
        _placeholderLable.frame = CGRectMake(5, self.frame.size.height - _textSize - 5 , self.frame.size.width - 10, _textSize);
        _placeholderLable.textAlignment = 2;
    }
}
@end
